﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.ComControls;
using MINERP.BLL.ComplexOrder;
using MINERP.MODEL;
using MINERP.MODEL.FoundationModel;
using MINERP.ComControls.Extension;

namespace MINERP.ComplexOrder.Frms
{
    public partial class FrmMain : FrmParent
    {
        public FrmMain()
        {
            InitializeComponent();
            //组合排单
            gridViewComplex.ShowRowIndicator(50);
            gridViewSunProduct.ShowRowIndicator(50);
            //排单生产
            gridViewProduct.ShowRowIndicator(50);
            gridViewSub.ShowRowIndicator(50);
        }
        ComplexOrderBll _bll = null;
        private void FrmMain_Load(object sender, EventArgs e)
        {
            _bll = new ComplexOrderBll();
            comboBoxType.SelectedIndex = 0;
            comboBoxSelectType.SelectedIndex = 1;
            TechDict = base.operates;
            if (tabControlmain.SelectedTab.Name == "tabPageComplexOrder")
            {
                int techID = TechDict.Where(d => d.Value == "组合排单").FirstOrDefault().Key;
                gridControlComplex.DataSource =CurrListComplex= _bll.GetAllComplexOrder(techID);

            }

            this.repositoryItemButtonEditProduct.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(repositoryItemButtonEditProduct_ButtonClick);
        }
        ComplexOrderInfo CurrProductOrder = new ComplexOrderInfo();
        List<ComplexOrderInfo> CurrListComplex = new List<ComplexOrderInfo>();
        void repositoryItemButtonEditProduct_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (CurrProductOrder == null)
            {
                MessageBox.Show("请选择产品");
                return;
            }
            CurrProductOrder = (ComplexOrderInfo)gridViewComplex.GetFocusedRow();
            FrmNumSure frm = new FrmNumSure();
            frm.PassNum += new FrmNumSure.PassSelectNum(frm_PassNum);
            frm.CurrProduct = CurrProductOrder;
            if (DialogResult.OK == frm.ShowDialog())
            {
                if (CurrProductOrder.OperNum > 0)
                {
                    //进行分单操作.如果分配数量小于产品要生产的数量在原来的订单上面创建新的产品信息并吧新建的产品的流程修改到下个一流程
                    //如果等于要生产的数量就把该产品的流程激活到下一个流程
                    bool isTrue = false;
                    if (CurrProductOrder.OperNum == CurrProductOrder.ProductNum)
                    {
                        int techID = TechDict.Where(d => d.Value == "组合排单").FirstOrDefault().Key;
                      isTrue=  _bll.MakeSureProduct(CurrProductOrder, techID, base.userId);
                      if (isTrue)
                      {
                          List<ComplexOrderInfo> List = (List<ComplexOrderInfo>)gridViewComplex.DataSource;
                          List.Remove(CurrProductOrder);
                          gridViewComplex.RefreshData();
                      }
                    }
                    else
                    {
                         int techID = TechDict.Where(d => d.Value == "组合排单").FirstOrDefault().Key;
                        isTrue= _bll.MakeSureProduct(CurrProductOrder, techID, base.userId);
                        if (isTrue)
                        {
                            CurrProductOrder.ProductNum -= CurrProductOrder.OperNum;
                        }
                    }
                    
 
                }
 
            }

        }

        void frm_PassNum(int SelectNum)
        {
            CurrProductOrder.OperNum = SelectNum;
            CurrProductOrder.ProductNum -= CurrProductOrder.OperNum;
        }
        Dictionary<int, string> TechDict = new Dictionary<int, string>();
       

        private void gridControlComplex_Click(object sender, EventArgs e)
        {

        }

        private void tabControlmain_SelectedIndexChanged(object sender, EventArgs e)
        {
            rushData();
        }
        void rushData()
        {
            if (tabControlmain.SelectedTab.Name == "tabPageComplexOrder")
            {
                int techID = TechDict.Where(d => d.Value.Equals("组合排单")).FirstOrDefault().Key;
                gridControlComplex.DataSource = _bll.GetAllComplexOrder(techID);

            }
            else
            {
                int techID = TechDict.Where(d => d.Value.Equals("排单生产")).FirstOrDefault().Key;
                gridControlProduct.DataSource = _bll.GetAllComplexOrder(techID);
                comboBoxSelectType.SelectedIndex = 0;
            }
        }
        private void gridViewComplex_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
        {
            if (e.RowHandle >= 0)
            {

                e.ChildList = ((ComplexOrderInfo)gridViewComplex.GetRow(e.RowHandle)).SubProductList;
            }
        }

        private void gridViewComplex_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gridViewComplex_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
        {
            e.IsEmpty = false;
        }

        private void gridViewComplex_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "子产品";
        }

        private void gridViewComplex_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if (e.RowHandle >= 0 && e.Column.Name == "gridColumn19")
            //{
 
            //}
        }

        private void buttonFenAll_Click(object sender, EventArgs e)
        {
            int techID = TechDict.Where(d => d.Value.Equals("组合排单")).FirstOrDefault().Key;
            if (MessageBox.Show("确认要将您选择的产品按照订单数量排单吗？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                int[]selectRowArry = gridViewComplex.GetSelectedRows();
                List<ComplexOrderInfo> selectOrder = new List<ComplexOrderInfo>();
                foreach (int rowID in selectRowArry)
                {
                    selectOrder.Add((ComplexOrderInfo)gridViewComplex.GetRow(rowID));
                }
                FrmYijian frm = new FrmYijian();
                frm.CurrList = selectOrder;
                frm._bLL = _bll;
                frm.userid = base.userId;
                frm.techid = techID;
                frm.ShowDialog();
                rushData();
            }
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {

        }

        private void gridViewProduct_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gridViewProduct_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "子产品";
        }

        private void gridViewProduct_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
        {
            if (e.RowHandle >= 0)
            {

                e.ChildList = ((ComplexOrderInfo)gridViewProduct.GetRow(e.RowHandle)).SubProductList;
            }
        }

        private void gridViewProduct_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
        {
            e.IsEmpty = false;
        }
        List<DisplayComplexOrderInfo> listNewSub = new List<DisplayComplexOrderInfo>();
        private void comboBoxSelectType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSelectType.SelectedIndex == 0)
            {
                int techID = TechDict.Where(d => d.Value.Equals("排单生产")).FirstOrDefault().Key;
                gridControlProduct.DataSource = _bll.GetAllComplexOrder(techID);
                gridControlProduct.MainView = gridViewProduct;
            }
            else
            {
                gridControlProduct.MainView = gridViewAll;
                int techID = TechDict.Where(d => d.Value.Equals("排单生产")).FirstOrDefault().Key;

                List<ComplexOrderInfo> list = _bll.GetAllComplexOrder(techID); ;
                listNewSub = new List<DisplayComplexOrderInfo>();
                foreach (ComplexOrderInfo coi in list)
                {
                    //DisplayComplexOrderInfo dcoi = new DisplayComplexOrderInfo();
                    foreach (ProductStoreInfo psi in coi.SubProductList)
                    {
                        DisplayComplexOrderInfo dcoi = new DisplayComplexOrderInfo();
                        dcoi.CtmName = coi.CtmName;
                        dcoi.AllNum = coi.ProductNum * psi.UnitNum;
                        dcoi.ClassName = psi.ClassName;
                        dcoi.OrderBagId = coi.OrderBagId;
                        dcoi.OrderProductName = coi.OrderProductName;
                        dcoi.OrderTime = coi.OrderTime;
                        dcoi.OrilNum = coi.OrilNum;
                        dcoi.Priority = coi.Priority;
                        dcoi.ProductName = psi.ProductName;
                        dcoi.ShopName = coi.ShopName;
                        dcoi.LockNum = psi.LockNum;
                        //dcoi.usefulNum = psi.usefulNum;
                        dcoi.Num = psi.Num;
                        listNewSub.Add(dcoi);
                    }
                }
                gridControlProduct.DataSource = listNewSub;

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog = new SaveFileDialog();
            //saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
            ////saveFileDialog.AddExtension = true;
            //saveFileDialog.FilterIndex = 3;
            //saveFileDialog.RestoreDirectory = true;
            //if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    string fileName = saveFileDialog.FileName;
            //    if (saveFileDialog.OpenFile() != null)
            //    {
            //        switch (saveFileDialog.FilterIndex)
            //        {
            //            case 3:
            //                gridViewAll.ExportToXls(fileName);
            //                return;
            //            case 4:
            //                gridViewAll.ExportToHtml(fileName);
            //                return;
            //            case 2:
            //                gridViewAll.ExportToPdf(fileName);
            //                return;
            //            case 1:
            //                this.gridViewAll.ExportToText(fileName);
            //                return;
            //            default:
            //                break;
            //        }
            //    }

            //}
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //saveFileDialog1.FileName = cmbGroup.Text+"组的员工业绩.xls";
                saveFileDialog1.Filter = "文本文件 (*.txt)|*.txt|pdf(*.pdf)|*.pdf|电子表格(*.xls)|*.xls|html(*.html)|*.html";
                string saveDir = saveFileDialog1.FileName;
                saveFileDialog1.FilterIndex = 3;
                saveFileDialog1.RestoreDirectory = true;
                if (!string.IsNullOrEmpty(saveDir))
                {
                    gridViewAll.ExportToXls(saveDir);
                    MessageBox.Show("导出成功!");
                }
            }
        }

        private void textEditCtmName_EditValueChanged(object sender, EventArgs e)
        {
         if(!string.IsNullOrEmpty(textEditCtmName.Text.Trim()))
         {

             gridControlComplex.DataSource = CurrListComplex.Where(p => p.ShopName.Contains(textEditCtmName.Text.Trim()));
         }
        }

        private void textEditProduct_EditValueChanged(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(textEditProduct.Text.Trim()))
         {

             gridControlComplex.DataSource = CurrListComplex.Where(p => p.OrderProductName.Contains(textEditProduct.Text.Trim()));
         }
        }

        private void textEdit2_EditValueChanged(object sender, EventArgs e)
        {
            string str = textEdit2.Text.Trim();
            if (!string.IsNullOrEmpty(str))
            {
                gridControlProduct.MainView = gridViewAll;
                gridControlProduct.DataSource = listNewSub.Where(s=>s.ShopName.Contains(str));
            }
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            string str = textEdit1.Text.Trim();
            if (!string.IsNullOrEmpty(str))
            {
                gridControlProduct.MainView = gridViewAll;
                gridControlProduct.DataSource = listNewSub.Where(s => s.ProductName.Contains(str));
            }
        }

        private void dateTimePickerEndTime_ValueChanged(object sender, EventArgs e)
        {
            DateTime stTime=Convert.ToDateTime(dateTimePickerStartTime.Text);
            DateTime endTime=Convert.ToDateTime(dateTimePickerEndTime.Text).AddDays(1);
            gridControlComplex.DataSource = CurrListComplex.Where(p => p.OrderTime >= stTime && p.OrderTime < endTime);
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime stTime = Convert.ToDateTime(dateTimePicker2.Text);
            DateTime endTime = Convert.ToDateTime(dateTimePicker1.Text).AddDays(1);
            gridControlProduct.MainView = gridViewAll;
            gridControlProduct.DataSource = listNewSub.Where(s => s.OrderTime>=stTime&&s.OrderTime<endTime);
        }
    }
    public class DisplayComplexOrderInfo : ComplexOrderInfo
    {
        public int Num { get; set; }
        public int SafeNum { get; set; }
        public int LockNum { get; set; }
        public string ClassName { get; set; }
        public int usefulNum { get { return Num - LockNum; } }
        public int UnitNum { get; set; }//单位需要数量
        public int AllNum { get;set; }
        public string ProductName { get; set; }
 
    }
}
